package com.ccmusic.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccmusic.Message.Msg;
import com.ccmusic.entity.User;
import com.ccmusic.service.UserService;
import com.ccmusic.service.impl.UserServiceImpl;
import com.google.gson.Gson;

/**
 * cyf用户注册的处理 Servlet implementation class ZhuCeServlet
 */
@WebServlet("/ZhuCeServlet")
public class ZhuCeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService us = new UserServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求编码
		request.setCharacterEncoding("utf-8");
		// 判断请求参数，执行注册方法
		if (request.getParameter("op").equals("zhuce")) {
			// System.out.println("注册");
			doZhuCe(request, response);
		} else if (request.getParameter("op").equals("updatePwd")) {
			/**
			 * 4.1 通过用户id修改密码操作---- wwb
			 */
			updatePwd(request, response);
		} else if (request.getParameter("op").equals("checkNewPwdReNewPwd")) {
			/**
			 * 4.1 校验修改的密码是否相同---- wwb
			 */
			checkNewPwdReNewPwd(request, response);
		}

	}

	private void checkNewPwdReNewPwd(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置请求编码格式
		request.setCharacterEncoding("utf-8");
		// 设置响应编码
		response.setCharacterEncoding("utf-8");
		// 设置响应数据类型
		response.setContentType("application/json");
		
		String oldPwd=request.getParameter("oldpwd");
		String newPwd=request.getParameter("newpwd");
		int uid=Integer.parseInt(request.getParameter("uid"));
		boolean result=us.updatePwd(uid, newPwd);
		Msg mess=new Msg();
		if(result) {
			mess.setMsg("修改成功");
		}else {
			mess.setMsg("修改失败");
		}
		
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		out.print(gson.toJson(mess));
		out.close();
	}

	/**
	 * 用户注册的方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doZhuCe(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求编码格式
		request.setCharacterEncoding("utf-8");
		// 设置响应编码
		response.setCharacterEncoding("utf-8");
		// 设置响应数据类型
		response.setContentType("application/json");
		String uname = "";
		String pwd = "";
		String usex = "";
		String tel = "";
		String birthday = "";
		Msg msg = new Msg();
		Gson gson = new Gson();
		// 判断用户输入的信息书否符合规范，并做相应处理
		if (request.getParameter("uname") != null && !request.getParameter("uname").equals("")
				&& request.getParameter("pwd") != null && !request.getParameter("pwd").equals("")) {
			uname = request.getParameter("uname");
			pwd = request.getParameter("pwd");
			usex = request.getParameter("usex");
			tel = request.getParameter("tel");
			birthday = request.getParameter("birthday");
			// 检查数据库中是否有相同的用户名
			User user = us.check(uname);
			if (user == null) {
				User newuser = new User(uname, pwd, usex, tel, birthday, "1", 2);
				// System.out.println(uname+","+pwd+","+usex+","+tel+","+birthday);
				boolean flag = us.addUser(newuser);
				if (flag) {
					msg.setMsg("增加成功");
				} else {
					msg.setMsg("增加失败");

				}

			} else {
				msg.setMsg("用户名重复");

			}

		} else {
			msg.setMsg("用户名或密码不能为空");

		}
		;

		String str = gson.toJson(msg);
		PrintWriter out = response.getWriter();
		out.print(str);
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
